package jpave.query_interface;
/***
 * Used for the Library Included/Exclude for at least N number of reads
 * Called from QueryTab
 */
import javax.swing.BoxLayout;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JLabel;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import javax.swing.event.CaretListener;
import javax.swing.event.CaretEvent;
import java.awt.ItemSelectable;

public class LibraryPromptField extends JPanel implements ItemSelectable {
	private static final long serialVersionUID = 7617905498520098841L;
	private ItemListener listener;
	private JLabel leadLabel = null, midLabel = null, endLabel1 = null, endLabel2 = null, endLabel3 = null;
	private JTextField numField = null;
	private JComboBox optionsCtrl1 = null, optionsCtrl2 = null, optionsCtrl3 = null;
	private double dNumVal = -1;
	private JCheckBox chkSelect = null;
	
	public LibraryPromptField(String leadText, double numVal, 
			String midText, String [] options1, String endText1, 
			String [] options2, String endText2, 
			String [] options3, String endText3,
			String statusText, Dimension fieldDim, 
			ItemListener l, int defaultOption1, int defaultOption2, int defaultOption3, boolean isSelectable)
	{
		dNumVal = numVal;
		listener = l;
		
		if(isSelectable) {
			chkSelect = new JCheckBox("");
			chkSelect.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					if (listener != null) {
						listener.itemStateChanged(new ItemEvent(LibraryPromptField.this, e.getID(), null, 0));
					}
				}
			});
		}
		
		if(leadText != null)	leadLabel = new JLabel(leadText);
		if(midText != null)	midLabel = new JLabel(midText);
		if(endText1 != null)	endLabel1 = new JLabel(endText1);
		if(endText2 != null)	endLabel2 = new JLabel(endText2);
		if(endText3 != null)	endLabel3 = new JLabel(endText3); // CAS 11/28/12 was endText2
		
		numField = new JTextField(Double.toString(dNumVal));
		numField.setPreferredSize ( fieldDim );
		numField.setMaximumSize( fieldDim );
		numField.setMinimumSize( fieldDim );
		numField.addCaretListener(new CaretListener() {
			public void caretUpdate(CaretEvent e) {
		    	if (listener != null) {
		    		try {
		    			dNumVal = Double.parseDouble(numField.getText());	
		    		}
		    		catch (NumberFormatException nfe) {
		    			dNumVal = 0;
		    		}
		    		listener.itemStateChanged( new ItemEvent(LibraryPromptField.this, e.getMark(), null, ItemEvent.ITEM_STATE_CHANGED));
		    	}
			}
		});
		
		if(options1 != null) {
			optionsCtrl1 = new JComboBox(options1);
			optionsCtrl1.setSelectedIndex(defaultOption1);
			optionsCtrl1.addItemListener(new ItemListener() {
				public void itemStateChanged(ItemEvent e) {
					if (listener != null) {
						listener.itemStateChanged(new ItemEvent(LibraryPromptField.this, e.getID(), null, e.getStateChange()));
					}
				}
			});
		}
		
		if(options2 != null) {
			optionsCtrl2 = new JComboBox(options2);
			optionsCtrl2.setSelectedIndex(defaultOption2);
			optionsCtrl2.addItemListener(new ItemListener() {
				public void itemStateChanged(ItemEvent e) {
					if (listener != null) {
						listener.itemStateChanged(new ItemEvent(LibraryPromptField.this, e.getID(), null, e.getStateChange()));
					}
				}
			});
		}
		
		if(options3 != null) {
			optionsCtrl3 = new JComboBox(options3);
			optionsCtrl3.setSelectedIndex(defaultOption3);
			optionsCtrl3.addItemListener(new ItemListener() {
				public void itemStateChanged(ItemEvent e) {
					if (listener != null) {
						listener.itemStateChanged(new ItemEvent(LibraryPromptField.this, e.getID(), null, e.getStateChange()));
					}
				}
			});
		}
		
		setLayout(new BoxLayout ( this, BoxLayout.X_AXIS ));
		super.setBackground(Color.WHITE);
		if(chkSelect != null) 	add(chkSelect);
		if(leadLabel != null) 	add(leadLabel);
		if(numField != null) 	add(numField);
		if(midLabel != null) 	add(midLabel);
		if(optionsCtrl1 != null) add(optionsCtrl1);
		if(endLabel1 != null) 	add(endLabel1);
		if(optionsCtrl2 != null) add(optionsCtrl2);
		if(endLabel2 != null) 	add(endLabel2);
		if(optionsCtrl3 != null) add(optionsCtrl3);
		if(endLabel3 != null) 	add(endLabel3);
	}
	
	public boolean isSelected() { 
		if(chkSelect != null)
			return chkSelect.isSelected();
		return false;
	}
	
	public void setSelected(boolean selected) {
		if(chkSelect != null)
			chkSelect.setSelected(selected);
	}
	
	public double getNum() { return dNumVal; }
	public void setNum(double val) { 
		dNumVal = val;
		numField.setText(dNumVal + "");
	}
	public int getOption1() { return optionsCtrl1.getSelectedIndex(); }
	public String getOption1Text() { return (String)optionsCtrl1.getSelectedItem(); }
	public void setOption1(int val) { optionsCtrl1.setSelectedIndex(val); }
	// CAS 11/28/12 added check for null
	public int getOption2() { return (optionsCtrl2==null) ? 0 : optionsCtrl2.getSelectedIndex(); }
	public String getOption2Text() { return (optionsCtrl2==null) ? " " : (String)optionsCtrl2.getSelectedItem(); }
	public void setOption2(int val) { if(optionsCtrl2!=null) optionsCtrl2.setSelectedIndex(val); }
	
	public int getOption3() { return (optionsCtrl3==null) ? 0 : optionsCtrl3.getSelectedIndex(); }
	public String getOption3Text() { return (optionsCtrl3==null) ? " " : (String)optionsCtrl3.getSelectedItem(); }
	public void setOption3(int val) { if(optionsCtrl3!=null) optionsCtrl3.setSelectedIndex(val); }

	public void addItemListener(ItemListener l) {
		listener = l;
	}
	
	public Object[] getSelectedObjects() {
		return null;
	}
	
	public void removeItemListener(ItemListener l) {
		listener = null;
	}

}
